/*
 * @Author: your name
 * @Date: 2022-03-11 23:09:38
 * @LastEditTime: 2022-03-16 16:12:41
 * @LastEditors: Please set LastEditors
 * @FilePath: \VUE\app\src\router\index.js
 */
//配置路由的地方
import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './router'

//引入store
import store from '../store'
//使用插件
Vue.use(VueRouter)


let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

//重写push和replace方法
//第一个参数是跳转的位置 第二个参数是成功的回调 第三个参数是失败的回调
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        //call和apply
        //call和apply都可以调用函数，都可以篡改函数的上下文一次
        //区别:call传递参数是用逗号隔开，apply传递参数是只能是数组
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, resolve => { }, reject => { })
    }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originReplace.replace(this, location, resolve, reject)
    } else {
        originReplace.replace(this, location, resolve => { }, reject => { })
    }
}


let router = new VueRouter({
    //配置路由
    routes,
    //滚动行为，控制路由跳转后滚动条在什么位置
    scrollBehavior(to, from, savedPosition) {
        return { x: 0, y: 0 }
    }
})
router.beforeEach((to, from, next) => {
    console.log(store);
    next()
})
export default router